/**
 * Created by Administrator on 2017/8/28 0028.
 */


deliverFormatter = function (value, row, index) {
    if (row.state == 1) {
        return [
            '<a href="javascript:void(0)" class="through">审核通过</a>' +
            '&nbsp;&nbsp;&nbsp;&nbsp;' +
            '<a href="javascript:void(0)" class="refuse">审核拒绝</a>' +
            '&nbsp;&nbsp;&nbsp;&nbsp;' +
            '<a class="tixianjilu" href="javascript:void(0)">查看提现记录</a>'
        ].join('');
    } else {
        return [
            '<a class="tixianjilu" href="javascript:void(0)">查看提现记录</a>'
        ].join('');
    }
}


dealNameFormatter = function (value, row, index) {
    if (value == null || value == "") {
        return '暂无';
    } else {
        return value;
    }
}

dealTimeFormatter = function (value, row, index) {
    if (value == null || value == "") {
        return '暂无';
    } else {
        return value;
    }
};


window.bussEvent = {
    'click .through':function (e, value, row) {
        comment.showOperationTips("确定审核通过吗？", function () {
            $.post($().AbsUrlParse("/web/cash/audit"), {id: row.id, state: 2}, function (data) {
                if (data.state == true) {
                    if (data.res.code == 40000) {
                        hint("操作成功,已经审核通过");
                        var table = $('#table');
                        table.bootstrapTable('refresh');
                    } else {
                        hint(data.res.msg);
                    }
                } else {
                    hint(data.msg);
                }
            });
        })
    },
    'click .refuse':function (e, value, row) {
        showModel("auditdisaggress-div", function () {
            $("#auditdisaggress-div input[name=auditid]").val(row.id);
            $("#auditdisaggress-div textarea[name=reason]").val("");
            $("#money").val(row.money);
            $("#deliveryId").val(row.deliveryId);
        });
    },
    'click .tixianjilu': function (e, value, row, index) {
        showModel("jiaoyijilu-div", function () {
            var url = $().AbsUrlParse("/web/cash/delivercashQuery?deliverId=" + row.deliveryId + "");
            $("#table1").bootstrapTable("refresh", {url: url});
        })
    }
};

bindClick("#auditdisaggress", function () {
    var id = $("#auditdisaggress-div input[name=auditid]").val();
    var reason = $("#auditdisaggress-div textarea[name=reason]").val();
    if (reason == "" || reason.trim() == "") {
        hint("请输入拒绝理由")
        return false;
    }
    var money = $("#money").val();
    var deliveryId = $("#deliveryId").val();
    $.post($().AbsUrlParse("/web/cash/audit"), {
        id: id,
        state: 3,
        reason: reason,
        money: money,
        deliveryId: deliveryId
    }, function (data) {
        if (data.state == true) {
            if (data.res.code == 40000) {
                closeModel("auditdisaggress-div");
                hint("操作成功,已经审核拒绝");
                var table = $('#table');
                table.bootstrapTable('refresh');
            } else {
                hint(data.res.msg);
            }
        } else {
            hint(data.msg);
        }
    });
})


var stateFormtter = function (value, row, index) {
    if (value == 1) {
        return '<span style="color:#FAB61F">待审核</span>';
    } else if (value == 2) {
        return '<span style="color:#00e765">审核通过</span>';
    } else if (value == 3) {
        return '<span style="color:red">审核拒绝</span>';
    }
}

//配送员的导出的功能
bindClick("#exportFinal", function () {

    var ids = getSelectedIds();

    if (ids == false) {
        return false;
    }

    $.post($().AbsUrlParse("/web/cash/delivercashQueryExport"), {
        ids: ids
    }, function (data) {
        if (data.state == true) {
            if (data.res.code == 40000) {
                hint("导出成功");
                var a = document.createElement("a");
                a.href = $().AbsUrlParse("/excel/" + data.res.data);
                a.click();
            } else {
                hint(data.res.msg);
            }
        } else {
            hint(data.msg);
        }
    });
});

/**
 * 获取选择的提现信息
 * @returns {*}
 */
var getSelectedIds = function () {
    var table = $("#table");
    var selectedRows = table.bootstrapTable('getSelections');
    var selectedRowsLength = selectedRows.length;
    if (selectedRowsLength == 0) {
        hint("请选择数据");
        return false;
    }
    var ids = "";
    for (var i = 0; i < selectedRowsLength; i++) {
        ids += selectedRows[i].id + ",";
    }
    ids = ids.substring(0, ids.length - 1);

    return ids;
}


bindClick("#btnSearch", function () {
    var bussNameS = $("input[name=bussNameS]").val();
    var timeS = $("input[name=timeS]").val();
    var cashstatus = $("select[name=stateS]").val();

    var start = "";
    var end = "";
    if (timeS != null) {
        start = timeS.split(" - ")[0];
        end = timeS.split(" - ")[1];
    }
    var url = $().AbsUrlParse("/web/cash/delivercashQuery?bussName=" + bussNameS + "&start=" + start + "&end=" + end + "&cashstatus=" + cashstatus);
    $("#table").bootstrapTable("refresh", {"url": url});
});

function allcheckbox() {
    var flag = $("#allcheckbox").is(":checked");
    if (flag) {
        $.each($("input[class=mycheckbox1]"), function () {
            $(this).prop("checked", true);
        });
    } else {
        $.each($("input[class=mycheckbox1]"), function () {
            $(this).prop("checked", false);
        });
    }
}


function initDateRange() {
    $(".rangedate").daterangepicker({
        "autoApply": true,
        "locale": {
            "applyLabel": "确定",
            "cancelLabel": "取消",
            "fromLabel": "开始",
            "toLabel": "结束",
            "customRangeLabel": "Custom",
            "weekLabel": "周",
            "daysOfWeek": [
                "日",
                "一",
                "二",
                "三",
                "四",
                "五",
                "六"
            ],
            "monthNames": [
                "1月",
                "2月",
                "3月",
                "4月",
                "5月",
                "6月",
                "7月",
                "8月",
                "9月",
                "10月",
                "11月",
                "12月"
            ],
            "cancelLabel": '清空',
            "opens": "right"
        },
        "format": "YYYY-MM-DD"
    }).on('cancel.daterangepicker', function (ev, picker) {
        $(this).val('');
    });
}

//初始化页面的时候运行该函数
$(function () {
    initDateRange();
});
